package com.cq.hd.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cq.hd.common.pagination.Page;
import com.cq.hd.common.response.Throw;
import com.cq.hd.common.utils.BeanUtils;
import com.cq.hd.common.utils.StringUtils;
import com.cq.hd.system.mapper.TbContentMapper;
import com.cq.hd.system.po.TbContentCategoryPo;
import com.cq.hd.system.mapper.TbContentCategoryMapper;
import com.cq.hd.system.po.TbContentPo;
import com.cq.hd.system.service.TbContentCategoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cq.hd.system.api.dto.ContentCategoryPageDto;
import com.cq.hd.system.api.dto.ContentCategorySaveDto;
import com.cq.hd.system.api.dto.ContentCategoryUpdateDto;
import com.cq.hd.system.api.vo.ContentCategoryPageVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 学院分类表 服务实现类
 * </p>
 *
 * @author mYunYu
 * @since 2023-11-19
 */
@Service
public class TbContentCategoryServiceImpl extends ServiceImpl<TbContentCategoryMapper, TbContentCategoryPo> implements TbContentCategoryService {

    @Autowired
    private TbContentMapper contentMapper;

    @Override
    public Page<ContentCategoryPageVo> pageContentCategory(ContentCategoryPageDto contentCategoryPageDto) {
        String startTime = contentCategoryPageDto.getStartTime();
        String endTime = contentCategoryPageDto.getEndTime();
        if (!StringUtils.isBlank(startTime) && startTime.length() != 19) {
            Throw.isBusinessException("日期格式错误");
        }
        if (!StringUtils.isBlank(endTime) && endTime.length() != 19) {
            Throw.isBusinessException("日期格式错误");
        }

        PageHelper.startPage(contentCategoryPageDto.getPageNum(), contentCategoryPageDto.getPageSize());
        List<ContentCategoryPageVo> contentCategoryPageVos = baseMapper.listContentCategoryByDto(contentCategoryPageDto);
        PageInfo<ContentCategoryPageVo> pageInfo = new PageInfo<>(contentCategoryPageVos);
        return Page.newPageWithAllFields(pageInfo.getList(), pageInfo.getPageNum(), pageInfo.getPageSize(), pageInfo.getTotal(), pageInfo.getPages());
    }

    @Override
    public Long saveContentCategory(ContentCategorySaveDto contentCategorySaveDto) {
        Integer categorySort = contentCategorySaveDto.getCategorySort();
        if (categorySort > 1000000) {
            Throw.isBusinessException("端口内排序不能超过1000000");
        }

        TbContentCategoryPo tbContentCategoryPo = new TbContentCategoryPo();
        BeanUtils.copyProperties(contentCategorySaveDto, tbContentCategoryPo);
        tbContentCategoryPo.setCreateTime(LocalDateTime.now());
        int insert = baseMapper.insert(tbContentCategoryPo);
        if (insert == 0) {
            Throw.isBusinessException("保存失败");
        }
        return tbContentCategoryPo.getId();
    }

    @Override
    public Boolean updateContentCategory(ContentCategoryUpdateDto contentCategoryUpdateDto) {
        Integer categorySort = contentCategoryUpdateDto.getCategorySort();
        if(categorySort > 1000000){
            Throw.isBusinessException("端口内排序不能超过1000000");
        }

        TbContentCategoryPo tbContentCategoryPo = baseMapper.selectById(contentCategoryUpdateDto.getId());
        if (tbContentCategoryPo == null) {
            Throw.isBusinessException("未找到分类数据");
        }

        BeanUtils.copyProperties(contentCategoryUpdateDto, tbContentCategoryPo);
        tbContentCategoryPo.setUpdateTime(LocalDateTime.now());
        int update = baseMapper.updateById(tbContentCategoryPo);
        if (update == 0) {
            Throw.isBusinessException("更新失败");
        }

        return true;
    }

    @Override
    public Boolean deleteContentCategory(Long id) {
        TbContentCategoryPo tbContentCategoryPo = baseMapper.selectById(id);
        if (tbContentCategoryPo == null) {
            Throw.isBusinessException("未找到分类数据");
        }

        // 判断该分类下是否存在文章数据，存在则不能删除
        Integer count = contentMapper.selectCount(new QueryWrapper<TbContentPo>().eq("del_state", 0).eq("category_id", id));
        if (count != null && count > 0) {
            Throw.isBusinessException("当前分类有关联的文章，不可删除");
        }

        tbContentCategoryPo.setDelState(1);
        tbContentCategoryPo.setUpdateTime(LocalDateTime.now());
        int update = baseMapper.updateById(tbContentCategoryPo);
        if (update == 0) {
            Throw.isBusinessException("删除失败");
        }

        return true;
    }
}
